﻿using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.BaseModule.UIEExtract
{
    /// <summary>
    /// UIE服务
    /// </summary>
    public class UIEServiceProxy
    {
        /// <summary>
        /// 使用paddlenlp提取数据
        /// </summary>
        /// <param name="modelCode">模型编码</param>
        /// <param name="originalText">文本内容</param> 
        /// <param name="xmlConfig">模型xml配置内容</param>
        /// <returns></returns>
        public UIEModelResponse UieRecognition(string modelCode,string originalText,string xmlConfig)
        {
            UIEModelResponse response = new UIEModelResponse();
            try
            {
                response.Code = "1";
                string re = string.Empty;
                string url = ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("uieextract") + "/api/uierecognition";
                var param = new
                {
                    modelCode = modelCode,
                    content = originalText,
                    xmlConfig = xmlConfig
                };
                string postData = JsonConvert.SerializeObject(param);
                re = HttpHelp.HttpPostJson(url, postData);
                if(string.IsNullOrWhiteSpace(re))
                {
                    response.Body = null;
                }
                else
                {
                    var uieResult= JsonConvert.DeserializeObject<UIEModelResponse>(re);
                    if(uieResult != null && uieResult.Code == "1" && uieResult.Body != null && uieResult.Body.Any())
                    {
                        response.Body = uieResult.Body;
                        response.Message = uieResult.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = "0";
                response.Message = ex.ToString();
                response.Body = null;
            }
            return response;
        }
    }
}
